VERSION = 0
PATCHLEVEL = 1
SUBLEVEL = 0
EXTRAVERSION = -rc1
NAME = MAHE NH Kernel

ARCH ?= x86
CROSS_COMPILE ?=

CC = $(CROSS_COMPILE)gcc
LD = $(CROSS_COMPILE)ld
AS = $(CROSS_COMPILE)as
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
STRIP = $(CROSS_COMPILE)strip

CFLAGS = -Wall -Wextra -std=gnu11 -ffreestanding -fno-stack-protector -fno-pic -mno-red-zone
CFLAGS += -Iinclude -O2 -g

LDFLAGS = -nostdlib -static -z max-page-size=0x1000

SRCDIR = .
ARCHDIR = arch/$(ARCH)

KERNEL = mahe-nh.elf
ISO = mahe-nh.iso

all: $(KERNEL)

include $(ARCHDIR)/Makefile

config:
	@echo "Creating default config..."
	@echo "/* Auto-generated config */" > include/config.h
	@echo "#ifndef _CONFIG_H" >> include/config.h
	@echo "#define _CONFIG_H" >> include/config.h
	@echo "#define CONFIG_ARCH_X86 1" >> include/config.h
	@echo "#define CONFIG_MULTIBOOT2 1" >> include/config.h
	@echo "#define CONFIG_EARLY_CONSOLE 1" >> include/config.h
	@echo "#define CONFIG_DEBUG 1" >> include/config.h
	@echo "#endif /* _CONFIG_H */" >> include/config.h
	@echo "Default configuration created."

clean:
	$(RM) -f $(KERNEL) $(ISO)
	$(MAKE) -C $(ARCHDIR) clean
	$(RM) -rf iso
	$(RM) -f include/config.h

distclean: clean

iso: $(KERNEL)
	mkdir -p iso/boot/grub
	cp $(KERNEL) iso/boot/
	cp grub.cfg iso/boot/grub/
	grub-mkrescue -o $(ISO) iso

.PHONY: all clean distclean config iso
